package com.puman.common;


import java.lang.reflect.Field;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;




import org.springframework.jdbc.core.simple.ParameterizedRowMapper;


/**
 * 将rs 结果集中的一行记录转换为Obejct 对象
 * @author xiawu
 *
 */
@SuppressWarnings("rawtypes")
public class ObjectRowMapper implements ParameterizedRowMapper{

	private Class className;

	public ObjectRowMapper(Class className) {
            this.className = className;
    }

    /*
     * 该类自动将数据库字段对应到Object中相应字段 要求：数据库与Object中字段名相同
     */
    public Object mapRow(ResultSet rs, int rowNum) throws SQLException {

            Object nt = new Object();
            // 获取父类的属性
            Field[] fatherfields = className.getSuperclass().getDeclaredFields();
            Field[] fields = className.getDeclaredFields();
            try {
            	nt=className.newInstance();
            	// 把父类的属性复制到子类中去
                	for (Field field : fatherfields) {
                		 // 如果结果中没有改field项则跳过
                        try {
                                rs.findColumn(field.getName());
                        } catch (Exception e) {
                                continue;
                        }
                        // 修改相应filed的权限
                        boolean accessFlag = field.isAccessible();
                        field.setAccessible(true);
                        String fieldName=field.getName();
                        System.err.println(fieldName);
                        String value = rs.getString(fieldName);  
                        value = (value == null ? "" : value);
                        setFieldValue(nt, field, value);
                        // 恢复相应field的权限
                        field.setAccessible(accessFlag);
			    	}
                	//为子类中的说有属性赋值
                    for (Field field : fields) {
                            // 如果结果中没有改field项则跳过
                            try {
                                    rs.findColumn(field.getName());
                            } catch (Exception e) {
                                    continue;
                            }
                            // 修改相应filed的权限
                            boolean accessFlag = field.isAccessible();
                            field.setAccessible(true);
                            String fieldName=field.getName();
                            System.err.println(fieldName);
                            String value = rs.getString(fieldName);  
                            value = (value == null ? "" : value);
                            setFieldValue(nt, field, value);
                            // 恢复相应field的权限
                            field.setAccessible(accessFlag);
                    }
            } catch (Exception e) {
                    e.printStackTrace();
            }
            return nt;
    }

    /*
     * 根据类型对具体对象属性赋值
     */
    public static void setFieldValue(Object form, Field field, String value) {

            String elemType = field.getType().toString();
            try {
                    if (elemType.indexOf("boolean") != -1
                                    || elemType.indexOf("Boolean") != -1) {
                            field.set(form, Boolean.valueOf(value));
                    } else if (elemType.indexOf("byte") != -1
                                    || elemType.indexOf("Byte") != -1) {
                            field.set(form, Byte.valueOf(value));
                    } else if (elemType.indexOf("char") != -1
                                    || elemType.indexOf("Character") != -1) {
                            field.set(form, Character.valueOf(value.charAt(0)));
                    } else if (elemType.indexOf("double") != -1
                                    || elemType.indexOf("Double") != -1) {
                            field.set(form, Double.valueOf(value));
                    } else if (elemType.indexOf("float") != -1
                                    || elemType.indexOf("Float") != -1) {
                            field.set(form, Float.valueOf(value));
                    } else if (elemType.indexOf("int") != -1
                                    || elemType.indexOf("Integer") != -1) {
                            field.set(form, Integer.valueOf(value));
                    } else if (elemType.indexOf("long") != -1
                                    || elemType.indexOf("Long") != -1) {
                            field.set(form, Long.valueOf(value));
                    } else if (elemType.indexOf("short") != -1
                                    || elemType.indexOf("Short") != -1) {
                            field.set(form, Short.valueOf(value));

                    } else if (elemType.indexOf("Date") != -1) {
                            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                            try {
                                    field.set(form, sdf.parse(value));
                            } catch (ParseException e) {
                                    e.printStackTrace();
                            }
                    } else {
                            field.set(form, (Object) value);
                    }
            } catch (IllegalAccessException e) {
                    e.printStackTrace();
            } catch (IllegalArgumentException e) {
                    e.printStackTrace();
            }
    }
public static void main(String[] args) {
	
}
}
